package sort;

/**
 * @author 03010570
 * @date 2020/07/27
 * describe:
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] ints = {5, 2, 3, 1};
        QuickSort quickSort = new QuickSort();
        quickSort.quickSort(ints,0,ints.length-1);
        for (int anInt : ints) {
            System.out.print(anInt +" ,");
        }
    }

    public void quickSort(int[] nums, int start, int end) {
        if (start >= end) {
            return;
        }
        int partition = partition(nums, start, end);
        quickSort(nums, start, partition - 1);
        quickSort(nums, partition + 1, end);
    }

    private int partition(int[] nums, int start, int end) {
        int pivot = nums[end];
        int i = start;
        for (int j = start; j < end; j++) {
            if (nums[j] < pivot) {
                swap(nums, i, j);
                i++;
            }
        }
        swap(nums, i, end);
        return i;
    }

    private void swap(int[] nums, int start, int end) {
        int temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
    }


}
